<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use app\wechat;
use think\Db;
use think\Request;
use app\wechat\controller\account\Prowechat;
use app\wechat\controller\Index;

class Tencent extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }
    public function tencentlist()
    {
        $catregory = Db::table('nx_category')
            ->where([
                'delstatus'=>1,
                'level'=>1
            ])
            ->select();
        //公众号授权
        $wechat = new Index();
        $wurl = $wechat->get_preauth_url();
        $this->assign('turl',$wurl);
        $this->assign('category',$catregory);
        return $this->fetch('tencent/tencentlist');
    }
    //获取公众号列表
    public function getTencentList()
    {
        $keyword = $this->request->post('keyword','');
        $category = $this->request->post('category','');
        $category2= $this->request->post('category2','');
        $page= $this->request->post('page');
        $size= $this->request->post('size');
        if($category !== ''){
            $map[] = ['category', '=', $category];
        }
        if($category2 !== ''){
            $map[] = ['category2','=', $category2];
        }
        $map[] = ['delstatus', '=', 1];
        $list = Db::table('nx_authorizer')
            ->where('name|ghid|company','like','%'.$keyword.'%')
            ->where($map)
            ->order('category','desc')
            ->page($page,$size)
            ->select();
        $pagecount = Db::table('nx_authorizer')
            ->where('name|ghid|company','like','%'.$keyword.'%')
            ->where($map)
            ->count();
        foreach($list as $k=>$v){
            if($v['category']!=''){
                $categorylist = Db::table('nx_category')->where('id',$v['category'])->find();
                $list[$k]['category'] = $categorylist['categoryname'];
                if($categorylist){
                    $catregory2 = Db::table('nx_category')->where('id',$v['category2'])->find();
                    $list[$k]['category2'] = $catregory2['categoryname'];
                }
            }else{
                $list[$k]['category'] = '';
                $list[$k]['category2'] = '';
            }
        }
        return  $this->api_resp_success("",array("count"=>$pagecount),$list);
    }
    //获取二级分类列表
    public function getCategoryList()
    {
        $id = $this->request->post('id','');
        $catregory = Db::table('nx_category')
            ->where([
                'delstatus'=>1,
                'level'=>2,
                'pid'=> $id,
            ])
            ->select();
        $this->assign('category',$catregory);
        return  $this->api_resp_success("",'',$catregory);
    }
    //删除
    public function delteTencent()
    {
        $ids = $this->request->post('ids');
        $idsarr = implode(',',$ids);
        $res = Db::table('nx_authorizer')->where('id','in',$idsarr)->delete();
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    //查看消息回复列表
    public function checkGzList()
    {
        $id = $this->request->param('tid');
        $data = Db::table('nx_authorizer')->where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch('tencent/checkGzList');
    }
    //查看消息回复列表
    public function checkPtList()
    {
        $id = $this->request->param('tid');
        $data = Db::table('nx_authorizer')->where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch('tencent/checkPtList');
    }
    public  function  getImgtextList()
    {
        $id = $this->request->post('id');
        $keyword = $this->request->post('keyword');
        $type = $this->request->post('type');
        if($type == 1){
            $list = Db::table('nx_authorizer')->where('id',$id)->field('id,info_gzid')->find();
            $info_gzid = explode(',',$list['info_gzid']);
        }else{
            $list = Db::table('nx_authorizer')->where('id',$id)->field('id,info_ptid')->find();
            $info_gzid = explode(',',$list['info_ptid']);
        }
        foreach ($info_gzid as $k=>$v){
            $data = Db::table('nx_imgtext_info')
                ->where('title','like','%'.$keyword.'%')
                ->where('id',$v)
                ->find();
            $reslist[$k] = $data;
        }
        return $this->api_resp_success('','',$reslist);
    }
    //删除
    public function deleteImgText()
    {
        $id = $this->request->post('tid');
        $idlist = $this->request->post('chk_value');
        $type = $this->request->post('type');
        if($idlist != ''){
            $idstr = implode(',',$idlist);
        }else{
            $idstr = $idlist;
        }
        if($type == 1){
            $data = [
                'info_gzid'=>$idstr,
                'updatetime'=>date('Y-m-d H:i:s',time())
            ];
            $res = Db::name('nx_authorizer')->where('id',$id)->update($data);
            return $this->api_resp_success('','',$res);
        }else{
            $data = [
                'info_ptid'=>$idstr,
                'updatetime'=>date('Y-m-d H:i:s',time())
            ];
            $res = Db::name('nx_authorizer')->where('id',$id)->update($data);
            return $this->api_resp_success('','',$res);
        }
    }
    //添加消息回复
    public function addImgText()
    {
        $id = $this->request->param('tid');
        $type = $this->request->param('type');
        $this->assign('tid',$id);
        $this->assign('type',$type);
        return $this->fetch('tencent/addImgText');
    }
    public function doAddImgtext()
    {
        $id = $this->request->post('tid');
        $chk_value = $this->request->post('chk_value');
        $chk_value = implode(',',$chk_value);
        $type = $this->request->post('type');
        $info = Db::table('nx_authorizer')->where('id',$id)->find();
        if($type == 1){
            if($info['info_gzid'] == ''){
                $check = $chk_value;
            }else{
                $check = $info['info_gzid'].','.$chk_value;
            }
            $data = [
                'info_gzid'=>$check,
                'updatetime'=>date('Y-m-d H:i:s',time())
            ];
        }else{
            if($info['info_ptid'] == ''){
                $check = $chk_value;
            }else{
                $check = $info['info_ptid'].','.$chk_value;
            }
            $data = [
                'info_ptid'=>$check,
                'updatetime'=>date('Y-m-d H:i:s',time())
            ];
        }
        $res = Db::name('nx_authorizer')->where('id',$id)->update($data);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    public function getaddImgtextList()
    {
        $id = $this->request->post('tid');
        $type = $this->request->post('type');
        $keyword = $this->request->post('keyword');
        if($type == 1){
            $elist = Db::table('nx_authorizer')->where('id',$id)->field('info_gzid')->find();
            $listarr = explode(',',$elist['info_gzid']);
            $data = Db::table('nx_imgtext_info')
                ->where('title','like','%'.$keyword.'%')
                ->where([
                    'delstatus'=>1,
                    'type'=>1
                ])->select();
            foreach($data as $k=>$v){
                if(!in_array($v['id'],$listarr)){
                    $list[$k]['id'] = $v['id'];
                    $list[$k]['title'] = $v['title'];
                    $list[$k]['desc'] = $v['desc'];
                    $list[$k]['img_url'] = $v['img_url'];
                    $list[$k]['t_url'] = $v['t_url'];
                    $list[$k]['info_type'] = $v['info_type'];
                }
            }
            return $this->api_resp_success('','',$list);
        }else{
            $elist = Db::table('nx_authorizer')->where('id',$id)->field('info_ptid')->find();
            $listarr = explode(',',$elist['info_ptid']);
            $data = Db::table('nx_imgtext_info')
                ->where('title','like','%'.$keyword.'%')
                ->where([
                    'delstatus'=>1,
                    'type'=>0
                ])->select();
            foreach($data as $k=>$v){
                if(!in_array($v['id'],$listarr)){
                    $list[$k]['id'] = $v['id'];
                    $list[$k]['title'] = $v['title'];
                    $list[$k]['desc'] = $v['desc'];
                    $list[$k]['img_url'] = $v['img_url'];
                    $list[$k]['t_url'] = $v['t_url'];
                    $list[$k]['info_type'] = $v['info_type'];
                }
            }
            return $this->api_resp_success('','',$list);
        }
    }
//    编辑账号
    public function edittencent()
    {
        $id = $this->request->get('tencentid','');
        $data = Db::table('nx_authorizer')->where('id', $id)->find();
        $category = Db::table('nx_category')
            ->where([
                'delstatus'=>1,
                'level'=>1,
            ])->select();
        $category2 = Db::table('nx_category')
            ->where([
                'delstatus'=>1,
                'level'=>2,
            ])->select();
        $this->assign('category',$category);
        $this->assign('category2',$category2);
        $this->assign('data',$data);
        return $this->fetch('tencent/edittencent');
    }
    public function  doEditTencent()
    {
        $id = $this->request->post('id');
        $name = $this->request->post('name');
        $password = $this->request->post('password');
        $category = $this->request->post('category');
        $category2 = $this->request->post('category2');
        $appid = $this->request->post('appid');
        $endtime = $this->request->post('endtime');
        $company = $this->request->post('company');
        $data = [
            'name'=>$name,
            'password'=>$password,
            'category'=>$category,
            'category2'=>$category2,
            'appid'=>$appid,
            'endtime'=>$endtime,
            'company'=>$company
        ];
        $res = Db::name('nx_authorizer')->where('id',$id)->update($data);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
//    添加分类页面
    public function addCategory()
    {
        $this->checklogin();
        return $this->fetch('tencent/addCategory');
    }
    public function getCategory()
    {
        $keyword= $this->request->post('keyword','');
        $list = Db::table('nx_category')
            ->where('categoryname|sort','like','%'.$keyword.'%')
            ->where('delstatus',1)
            ->order('sort','asc')
            ->select();
        foreach ($list as $k=>$v){
            $name = Db::table('nx_category')->where('id',$v['pid'])->field('categoryname')->find();
            if($name){
                $list[$k]['superior'] = $name['categoryname'];
            }else{
                $list[$k]['superior'] = '';
            }
        }
        return $this->api_resp_success('','',$list);
    }
    public function doAddCategory()
    {
        $list = Db::table('nx_category')
            ->where([
                'delstatus'=>1,
                'level'=>1
            ])
            ->select();
        $this->assign('list',$list);
        return $this->fetch('tencent/doAddCategory');
    }
    public function doDeleteCategory()
    {
        $check = $this->request->post('chk_value');
        $res = Db::name('nx_category')
            ->where('id','in',$check)
            ->update(['delstatus'=>4]);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    public function doCategory()
    {
        $category = $this->request->post('category');
        $select1 = $this->request->post('select1');
        $select2 = $this->request->post('select2');
        $sort = $this->request->post('sort');
        $data = [
            'categoryname'=>$category,
            'level'=>$select1,
            'pid'=>$select2,
            'sort'=>$sort,
            'createtime'=>date('Y-m-d H:i:s',time())
        ];
        $res = Db::name('nx_category')->insert($data);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    public function addgroup()
    {
        $categoryid = $this->request->param('categoryid','');
        $pid = $this->request->param('pid','');
        $this->assign('pid',$pid);
        $this->assign('categoryid',$categoryid);
        return $this->fetch('tencent/addgroup');
    }
    public function doSavegroup()
    {
        $category1 = $this->request->post('category1','');
        $category2 = $this->request->post('category2','');
        $chk_value = $this->request->post('chk_value','');
        if($chk_value == ''){
            return false;
        }
        $data = [
            'category'=>$category1,
            'category2'=>$category2
        ];
        $res = Db::name('nx_authorizer')
            ->where('id','in',$chk_value)
            ->update($data);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    public function  getGroupList()
    {
        $keyword = $this->request->post('keyword','');
        $list1 = Db::table('nx_authorizer')
            ->where('name|ghid|company','like','%'.$keyword.'%')
            ->where('category2',null)
            ->order('createtime','desc')
            ->select();
        $list2 = Db::table('nx_authorizer')
            ->where('name|ghid|company','like','%'.$keyword.'%')
            ->where('category2','')
            ->order('createtime','desc')
            ->select();
        $list = array_merge($list1,$list2);
        return  $this->api_resp_success("",'',$list);
    }
    public function editCategory()
    {
        $id = $this->request->param('categoryid');
        $data = Db::table('nx_category')->where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch('tencent/editCategory');
    }
    public function doEditCategory()
    {
        $data = $this->request->param();
        $list = [
            'categoryname'=>$data['category'],
            'sort'=>$data['sort'],
            'updatetime'=>date('Y-m-d H:i:s',time())
        ];
        $res = Db::name('nx_category')->where('id',$data['id'])->update($list);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
}
